﻿using IceZero.DataStore;
using IceZero.DataStore.Models;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace IceZero.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class UserController : ControllerBase
    {
        private readonly UserDataStore _userDataStore;
        public UserController(UserDataStore userDataStore)
        {
            _userDataStore = userDataStore;
        }

        [HttpGet("GetUserList")]
        public List<User> GetUserList()
        {
            return _userDataStore.GetUserList();
        }

        [HttpGet("GetOnlineUserList")]
        public List<User> GetOnlineUserList()
        {
            return _userDataStore.GetOnlineUserList();
        }

        [HttpPost("AddUser")]
        [HttpPost("RegisterUser")]
        public void AddUser(User user)
        {
            _userDataStore.AddUser(user);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="user"></param>
        [HttpPost("DeleteUser")]
        [HttpPost("CloseUser")]
        public void DeleteUser(string guid)
        {
            var user = _userDataStore.QueryById(guid);
            if (user == null)
            {
                return;
            }
            _userDataStore.DeleteUser(user);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpGet("Login")]
        public string Login(string account, string password)
        {
            return _userDataStore.Login(account, password);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="token"></param>
        [HttpGet("Logout")]
        public void Logout(string token)
        {
            _userDataStore.Logout(token);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="token"></param>
        [HttpGet("IsLogin")]
        public bool IsLogin(string token)
        {
            return _userDataStore.IsLogin(token);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        [HttpPost("/api/test/[controller]/AddMockUser")]
        public IEnumerable<User> AddMockUser(int count)
        {
            var list = new LinkedList<User>();

            for (int i = 0; i < count; i++)
            {
                var user = new User()
                {
                    Account = "user" + i,
                    CreateTime = Faker.Generators.DateTimes.GetDateTime(),
                    Name = Faker.Generators.Names.FullName(),
                    Password = Faker.Generators.Strings.GenerateAlphaNumericString(6, 6),
                    Remark = Faker.Generators.EmailAddresses.Human(majorDomainExtensionsOnly: true)
                };
                _userDataStore.Save(user);
                list.AddLast(user);
            }
            return list;
        }
    }
}
